package com.frogs.ucenter.controller;

import java.util.Arrays;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.PageList;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.util.ExpandCodeEnum;
import com.frogs.system.util.IntegralExchangeColumnEnum;
import com.frogs.system.util.IntegralExchangeTargetEnum;
import com.frogs.system.util.UserAccountInfoStatusEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.service.GoodsService;
import com.frogs.ucenter.service.InsuranceService;
import com.frogs.ucenter.service.IntegralService;
import com.frogs.ucenter.service.UserIntegralService;
import com.frogs.ucenter.service.UserService;

/**
 * 积分商城
 * @author wushubin
 *
 */
@Controller
public class IntegralController extends BaseController3 {
	private static Logger log = LoggerFactory.getLogger(IntegralController.class);    
	
	public static final String URI_INDEX = "/integral/index";
    private static final String INDEX_VIEW = "integral/index";
    
    public static final String URI_VIP = "/integral/vip/index";
    public static final String URI_GOODS = "/integral/goods/index";
    public static final String URI_INSURANCE = "/integral/insurance/index";
    
    public static final String URI_INTEGRAL_COUPON_LIST = "/integral/coupon/list";
    public static final String URI_INTEGRAL_INSURANCE_LIST = "/integral/insurance/list";
    private static final String VIP_VIEW = "integral/vip_index";
    private static final String GOODS_VIEW = "integral/goods_index";
    private static final String INSURANCE_VIEW = "integral/insurance_index";
    
    public static final String INTEGRAL_EXCHANGE_COUPON = "/integral/exchange/coupon";
    public static final String INTEGRAL_EXCHANGE_CDKEY = "/integral/exchange/cdkey";
    
    @Resource
    private UserPermissionHelper userPermissionHelper;
    @Resource
    private GoodsService goodsService;
    @Resource
    private IntegralService integralService;
    
    @Resource
    private UserService userService;
    
    @Resource
    private UserIntegralService userIntegralService;
    
    @Resource
    private InsuranceService insuranceService;
    
    @RequestMapping(URI_INDEX)
    public String index(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
		try {
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			model.put("userInfo",user);
			model.put("integralRecordList", userIntegralService.getIntegralConsumeRecordList(1,8));
			model.put("integralInfo", userIntegralService.getUserIntegralInfo(user.getId()));
			model.put("recommendGoods", goodsService.getRecommendGoods(user.getId(), 6));
		} catch (Exception e) {
			log.error("func[index] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
        return INDEX_VIEW;
    }
    
    @RequestMapping(URI_GOODS)
    public String goodsIndex(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
    	
		try {
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			model.put("recommendGoods", goodsService.getRecommendGoods(user.getId(), 6));
			model.put("userInfo",user);
			model.put("integralRecordList", userIntegralService.getIntegralConsumeRecordList(1,8));
			model.put("integralInfo", userIntegralService.getUserIntegralInfo(user.getId()));
		} catch (Exception e) {
			log.error("func[goodsIndex] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
        return GOODS_VIEW;
    }
    @RequestMapping(URI_INSURANCE)
    public String insuranceIndex(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
    	
    	try {
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		model.put("recommendGoods", goodsService.getRecommendGoods(user.getId(), 6));
    		model.put("userInfo",user);
    		model.put("integralRecordList", userIntegralService.getIntegralConsumeRecordList(1,8));
    		model.put("integralInfo", userIntegralService.getUserIntegralInfo(user.getId()));
    	} catch (Exception e) {
    		log.error("func[insuranceIndex] parameterMap[{}] exception[{} - {}] desc[fail]",//
    				new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
    		
    		request.setAttribute("errorMessage", e.getMessage());
    		return ERROR_VIEW;
    	}
    	return INSURANCE_VIEW;
    }
    
    @RequestMapping(URI_VIP)
    public String vipIndex(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
    	
		try {
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			model.put("recommendGoods", goodsService.getRecommendGoods(user.getId(), 6));
			model.put("userInfo",user);
			model.put("integralRecordList", userIntegralService.getIntegralConsumeRecordList(1,8));
			model.put("integralInfo", userIntegralService.getUserIntegralInfo(user.getId()));
		} catch (Exception e) {
			log.error("func[vipIndex] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
        return VIP_VIEW;
    }
    
    @RequestMapping(URI_INTEGRAL_COUPON_LIST)
    public JSONObject getIntegralCouponList(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
    	HttpRequestInfo requestInfo = new HttpRequestInfo(request);
    	Integer pageNo = requestInfo.getIntParameter("pageNo", 1);
    	Integer pageSize = requestInfo.getIntParameter("pageSize", 12);
    	Integer isVip = requestInfo.getIntParameter("isVip", UserAccountInfoStatusEnum.VIP_NO.getId());
    	Integer columnId = requestInfo.getIntParameter("columnId",IntegralExchangeColumnEnum.VIP.getId());
    	JSONObject result = new JSONObject();
    	try {
    		PageList<Map<String, Object>> list = integralService.getIntegralCouponList(IntegralExchangeTargetEnum.COUPON.getId(),columnId, isVip, pageNo, pageSize);
    		result.put("couponList", list);
    		result.put("pageTurn", list.getPageTurn());
    	} catch (Exception e) {
    		log.error("func[getIntegralCouponList] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
    	return result;
    }
    @RequestMapping(URI_INTEGRAL_INSURANCE_LIST)
    public JSONObject getIntegralInsuranceList(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
    	HttpRequestInfo requestInfo = new HttpRequestInfo(request);
    	Integer pageNo = requestInfo.getIntParameter("pageNo", 1);
    	Integer pageSize = requestInfo.getIntParameter("pageSize", 12);
    	Integer isVip = requestInfo.getIntParameter("isVip", UserAccountInfoStatusEnum.VIP_NO.getId());
    	Integer columnId = requestInfo.getIntParameter("columnId",IntegralExchangeColumnEnum.VIP.getId());
    	
    	JSONObject result = new JSONObject();
    	try {
    		PageList<Map<String, Object>> list = insuranceService.getIntegralInsuranceList(IntegralExchangeTargetEnum.CDKEY.getId(),columnId, isVip, pageNo, pageSize);
    		result.put("insuranceList", list);
    		result.put("pageTurn", list.getPageTurn());
    	} catch (Exception e) {
    		log.error("func[getIntegralInsuranceList] parameterMap[{}] exception[{} - {}] desc[fail]",//
    				new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
    	}
    	return result;
    }
    
    /**
     * 积分兑换优惠券
     * 
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(INTEGRAL_EXCHANGE_COUPON)
    public JSONObject UserIntegralExchangeCoupon(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
    	HttpRequestInfo requestInfo = new HttpRequestInfo(request);
    	Integer id = requestInfo.getIntParameter("id", 0);
    	JSONObject result = new JSONObject();
    	try {
    		
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		JSONObject map = integralService.userIntegralCouponExchange(user.getId(), id);
    		result.put("code", map.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()));
    		result.put("tip", map.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
    	} catch (Exception e) {
    		log.error("func[UserIntegralExchangeCoupon] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
    	return result;
    }
    /**
     * 积分兑换保险激活码
     * 
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(INTEGRAL_EXCHANGE_CDKEY)
    public JSONObject UserIntegralExchangeCdkey(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
    	HttpRequestInfo requestInfo = new HttpRequestInfo(request);
    	JSONObject result = new JSONObject();
    	try {
    		
    		Integer id = requestInfo.getIntParameter("id", 0);
    		String expandCode = requestInfo.getParameter("expandCode", ExpandCodeEnum.MALL.getId());
    		Integer applicablePlatformId = requestInfo.getIntParameter("applicablePlatformId", ApplicablePlatformEnum.WEB.getId());
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		JSONObject map = integralService.cdkeyExchange(user.getId(), id, expandCode, applicablePlatformId);
    		result.put("code", map.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()));
    		result.put("tip", map.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
    	} catch (Exception e) {
    		log.error("func[UserIntegralExchangeCdkey] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
    	return result;
    }
}
